//
//  KYSystemConfigModel.m
//  KYKJPackDemo
//
//  Created by 罗新 on 2017/11/8.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "KYSystemConfigModel.h"
#import "KYAppConfigModel.h"
#import "KYAdvertZoneModel.h"
#import "KYAdvertRestrictionModel.h"
#import "KYCpsSchemeModel.h"
#import "YYCache.h"

@implementation KYSystemConfigModel
+ (instancetype)shareInstance{
    static KYSystemConfigModel *systemConfig;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        YYCache *cache = [YYCache cacheWithName:@"KYCache"];
        NSString *cacheKey = @"KYSystemCongfig";
        BOOL isContain = [cache containsObjectForKey:cacheKey];
        if (isContain) {
            systemConfig = (KYSystemConfigModel *)[cache objectForKey:cacheKey];
        }else{
           systemConfig = [[self alloc] init];
        }
    });
    return systemConfig;
}
- (instancetype)refreshDateWithDict:(NSDictionary *)dict{
    if ([dict[@"status"] isEqualToNumber:@1]) {
        NSDictionary *systemConfigDataDic = dict[@"data"];
        //系统配置
        if ([systemConfigDataDic[@"app_config"][@"status"] isEqualToNumber:@1]) {
            self.appConfig = [KYAppConfigModel refreshAppConfigDataWithDict:systemConfigDataDic[@"app_config"]];
        }
        //广告位
        NSArray *zoneArr = systemConfigDataDic[@"zone"];
        NSMutableArray *zoneArrM = [NSMutableArray array];
        for (NSDictionary *zoneDic in zoneArr) {
            KYAdvertZoneModel *model = [KYAdvertZoneModel refreshDataWithDict:zoneDic];
            [zoneArrM addObject:model];
        }
        self.advertZoneArr = [zoneArrM copy];
        //广告拦截
        if ([systemConfigDataDic[@"adv_restrictions"][@"status"] isEqualToNumber:@1]) {
            NSArray *adRestrictionArr = systemConfigDataDic[@"adv_restrictions"][@"newvalue"];
            NSMutableArray *adRestrictionArrM = [NSMutableArray array];
            for (NSDictionary *adRestrictionDic in adRestrictionArr) {
                KYAdvertRestrictionModel *model = [KYAdvertRestrictionModel refreshDataWithDict:adRestrictionDic];
                [adRestrictionArrM addObject:model];
            }
            self.advertRestrictionArr = [adRestrictionArrM copy];
        }
        //cps协议头拦截
        if ([systemConfigDataDic[@"scheme"][@"status"] isEqualToNumber:@1]) {
            NSArray *schemeArr = systemConfigDataDic[@"scheme"][@"data"];
            NSMutableArray *schemeArrM = [NSMutableArray array];
            for (NSDictionary *schemeDic in schemeArr) {
                KYCpsSchemeModel *model = [KYCpsSchemeModel refreshDataWithDict:schemeDic];
                [schemeArrM addObject:model];
            }
            self.cpsSchemeArr = [schemeArrM copy];
        }
    }
    return self;
}
- (void)encodeWithCoder:(NSCoder*)aCoder {
    unsigned int count = 0;
    Ivar* ivars = class_copyIvarList([self class], &count);
    for (int i = 0; i < count; i++) {
        Ivar ivar = ivars[i];
        const char* name = ivar_getName(ivar);
        NSString* key = [NSString stringWithUTF8String:name];
        [aCoder encodeObject:[self valueForKey:key] forKey :key];
    }
    free(ivars);
}
- (id)initWithCoder:(NSCoder*)aDecoder {
    if (self == [super init]) {
        unsigned int count = 0;
        Ivar* ivars = class_copyIvarList([self class], &count);
        for (int i = 0; i < count; i++) {
            Ivar ivar = ivars[i];
            const char* name = ivar_getName(ivar);
            NSString* key = [NSString stringWithUTF8String:name];
            id value =  [aDecoder decodeObjectForKey:key];
            [self setValue:value forKey:key];
        }
        free(ivars);
    }
    return self;
}
@end
